<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="开始时间">
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间" v-model="queryParam.startTime"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="结束时间">
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间" v-model="queryParam.endTime"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="当前是否超定额">
              <j-dict-select-tag placeholder="请选择当前是否超定额" v-model="queryParam.sfcde" dictCode="lgdk_sfcde"/>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="个体户姓名">
                <a-input placeholder="请输入个体户姓名" v-model="queryParam.gthxm"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="纳税人识别号">
                <a-input placeholder="请输入纳税人识别号" v-model="queryParam.nsrsbh"></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" key="1" v-has="'invoice:kjfp'"  @click="plkp()" style="margin-left: 8px"> 发起开票申请 </a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">开具发票</a>
          <a-divider type="vertical" />
          <a @click="handleDetail(record)">详情</a>
        </span>

      </a-table>
    </div>

    <lhyg-lgdk-invoice-modal ref="modalForm" @ok="modalFormOk"/>
  </a-card>
</template>

<script>

  import { httpAction, getAction, postAction } from '@/api/manage'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import LhygLgdkInvoiceModal from './modules/LhygLgdkInvoiceModal'
  import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
  import JDate from '@/components/jeecg/JDate.vue'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import '@/assets/less/TableExpand.less'
  import { JVXETypes } from '../../../components/jeecg/JVxeTable'
  import { FormTypes } from '../../../utils/JEditableTableUtil'

  export default {
    name: "LhygLgdkInvoiceList",
    mixins:[JeecgListMixin],
    components: {
      JDictSelectTag,
      JDate,
      LhygLgdkInvoiceModal
    },
    data () {
      return {
        description: '灵工代开发票管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'个体户姓名',
            align:"center",
            dataIndex: 'gthxm'
          },
          {
            title:'纳税人识别号',
            align:"center",
            dataIndex: 'nsrsbh'
          },
          {
            title:'货物或应税劳务、服务名称',
            align:"center",
            dataIndex: 'fwmc'
          },
          {
            title:'开票金额',
            align:"center",
            dataIndex: 'kpje'
          },
          {
            title:'当前是否超定额',
            align:"center",
            dataIndex: 'sfcde_dictText'
          },
          {
            title:'当前待开票金额',
            align:"center",
            dataIndex: 'dkpje'
          },
          {
            title:'开始时间',
            align:"center",
            dataIndex: 'startTime',
            customRender:function (text,record, index) {
              if(record.startTime == null) {
                return "不限制"
              } else {
                return text
              }
            }
          },
          {
            title:'结束时间',
            align:"center",
            dataIndex: 'endTime',
            customRender:function (text,record, index) {
              if(record.endTime == null) {
                return "不限制"
              } else {
                return text
              }
            }
          },
          {
            title:'工商户名称',
            align:"center",
            dataIndex: 'gshmc'
          },
          {
            title:'发布企业税号',
            align:"center",
            dataIndex: 'sh'
          },
          {
            title:'发布企业名称',
            align:"center",
            dataIndex: 'gsmc'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: "/lgdkinvoice/lhygLgdkInvoice/list",
          delete: "/lgdkinvoice/lhygLgdkInvoice/delete",
          deleteBatch: "/lgdkinvoice/lhygLgdkInvoice/deleteBatch",
          exportXlsUrl: "/lgdkinvoice/lhygLgdkInvoice/exportXls",
          importExcelUrl: "lgdkinvoice/lhygLgdkInvoice/importExcel",
          plkpUrl: "/lgdkinvoice/lhygLgdkInvoice/plkp",

        },
        dictOptions:{},
      }
    },
    created() {
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      initDictConfig(){
      },
      //批量开票
      plkp(){
        const that = this;
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请选择一条记录！');
          return;
        } else {
          var nsrsbhs = "";
          var gshmcs = "";
          var kpjes = "";
          var gthxms = "";
          var fwmcs = "";
          var shs = "";
          var gsmcs = "";
          var aLlength = this.selectedRowKeys.length;
          for (var a = 0; a < aLlength; a++) {
            if (this.selectionRows[a].nsrsbh != null && this.selectionRows[a].nsrsbh != undefined && this.selectionRows[a].nsrsbh != '') {
              if(a==aLlength-1){
                  nsrsbhs += this.selectionRows[a].nsrsbh;
                  gshmcs += this.selectionRows[a].gshmc;
                  kpjes += this.selectionRows[a].kpje;
                  gthxms += this.selectionRows[a].gthxm;
                  fwmcs += this.selectionRows[a].fwmc;
                  shs += this.selectionRows[a].sh;
                  gsmcs += this.selectionRows[a].gsmc;
              }else{
                  nsrsbhs += this.selectionRows[a].nsrsbh + ",";
                  gshmcs += this.selectionRows[a].gshmc + ",";
                  kpjes += this.selectionRows[a].kpje + ",";
                  gthxms += this.selectionRows[a].gthxm + ",";
                  fwmcs += this.selectionRows[a].fwmc + ",";
                  shs += this.selectionRows[a].sh + ",";
                  gsmcs += this.selectionRows[a].gsmc + ",";
              }
            }
          }
          if (nsrsbhs == "") {
            this.$message.warning('请选择至少一条已完成工商税务登记的记录！');
            return;
          }

          var startTime = this.queryParam.startTime;
          var endTime = this.queryParam.endTime;
          let formData=new FormData();
          formData.append('nsrsbhs', nsrsbhs);
          formData.append('gshmcs', gshmcs);
          formData.append('fwmcs', fwmcs);
          formData.append('gthxms', gthxms);
          formData.append('kpjes', kpjes);
          formData.append('startTime', startTime);
          formData.append('endTime', endTime);
          formData.append('shs', shs);
          formData.append('gsmcs', gsmcs);
          postAction(this.url.plkpUrl,formData).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              this.onClearSelected();
              this.loadData();
            }else {
              that.$message.error(res.message)
              this.onClearSelected();
              this.loadData();
            }
          });
        }
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
